﻿using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using OutlookPhoto;

// TODO:  Follow these steps to enable the Ribbon (XML) item:

// 1: Copy the following code block into the ThisAddin, ThisWorkbook, or ThisDocument class.

//  protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
//  {
//      return new Ribbon1();
//  }

// 2. Create callback methods in the "Ribbon Callbacks" region of this class to handle user
//    actions, such as clicking a button. Note: if you have exported this Ribbon from the Ribbon designer,
//    move your code from the event handlers to the callback methods and modify the code to work with the
//    Ribbon extensibility (RibbonX) programming model.

// 3. Assign attributes to the control tags in the Ribbon XML file to identify the appropriate callback methods in your code.  

// For more information, see the Ribbon XML documentation in the Visual Studio Tools for Office Help.


namespace OutlookPhotoAddIn
{
    [ComVisible(true)]
    public class OutlookPhotoAddin : Office.IRibbonExtensibility
    {
        private Outlook.Application olApplication;

        //Override of constructor to pass 
        // a trusted Outlook.Application object
        public OutlookPhotoAddin(Outlook.Application outlookApplication)
        {
            olApplication = outlookApplication as Outlook.Application;
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            string customUI = string.Empty;
            Debug.WriteLine(ribbonID);
            //Return the appropriate Ribbon XML for ribbonID
            return GetResourceText(
                "OutlookPhotoAddIn.OutlookPhoto.xml");
        }

        #endregion

        #region Ribbon Callbacks

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            ThisAddIn._Ribbon = ribbonUI;
        }

        public stdole.IPictureDisp GetCurrentUserImage(Office.IRibbonControl control)
        {
            Outlook.AddressEntry addrEntry =
                Globals.ThisAddIn.Application.Session.CurrentUser.AddressEntry;
            if (addrEntry.Type == "EX")
            {
                if (Globals.ThisAddIn.Pictdisp != null)
                {
                    return Globals.ThisAddIn.Pictdisp;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        // OnChangePhotoButtonClick routine handles all button click events
        public void OnChangePhotoButtonClick(Office.IRibbonControl control)
        {
            string username = null;

            Outlook.AddressEntry addrEntry =
                Globals.ThisAddIn.Application.Session.CurrentUser.AddressEntry;
            if (addrEntry != null)
            {
                if (addrEntry.Type == "EX")
                {
                    Outlook.ExchangeUser exchUser =
                        addrEntry.GetExchangeUser() as Outlook.ExchangeUser;
                    username = exchUser.Name;
                }
            }

            OutlookPhotoForm photoForm = new OutlookPhotoForm(username == null ? null : new string[] { username });
            photoForm.ShowDialog();
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}
